class AdvertismentAttributeFieldValue < ActiveRecord::Base
  belongs_to :advertisment
  belongs_to :university
  serialize :field_values

  def self.generate_list(university_id,category)

 
    hash_to_return = Hash.new
    find(:all,:conditions=>"university_id="+university_id.to_s+" and  category_id="+category.to_s).each do |aafv|
      field_value = aafv.field_values
      if field_value.class == Hash

        field_value.each do |key,value|
          hash_to_return[key] = Hash.new if hash_to_return[key].nil?

          if hash_to_return[key].keys.include?(value)
            hash_to_return[key][value]["count"] = (hash_to_return[key][value]["count"].to_i + 1)
            hash_to_return[key][value]["advertisments"] << aafv.advertisment_id
          else
            hash_to_return[key][value] = Hash.new
            hash_to_return[key][value]["count"] = 1
            hash_to_return[key][value]["advertisments"] = Array.new
            hash_to_return[key][value]["advertisments"] << aafv.advertisment_id
          end
          
        end
      end
    end

    return hash_to_return
  end

   def self.generate_list_sub_category_wise(university_id,sub_category)


    hash_to_return = Hash.new
    find(:all,:conditions=>"university_id="+university_id.to_s+" and  sub_category_id="+sub_category.to_s).each do |aafv|
      field_value = aafv.field_values
      if field_value.class == Hash

        field_value.each do |key,value|
          hash_to_return[key] = Hash.new if hash_to_return[key].nil?

          if hash_to_return[key].keys.include?(value)
            hash_to_return[key][value]["count"] = (hash_to_return[key][value]["count"].to_i + 1)
            hash_to_return[key][value]["advertisments"] << aafv.advertisment_id
          else
            hash_to_return[key][value] = Hash.new
            hash_to_return[key][value]["count"] = 1
            hash_to_return[key][value]["advertisments"] = Array.new
            hash_to_return[key][value]["advertisments"] << aafv.advertisment_id
          end

        end
      end
    end

    return hash_to_return
  end

end
